package com.shanshi.activity.tool;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shanshi.activity.R;
import com.shanshi.activity.request.EssayInfo;
import com.shanshi.activity.response.SendAnswer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cz on 2017/3/20.
 */

public class CheckView extends LinearLayout{

    private RadioGroup radioGroup;
    private TextView tv;
    private TextView tv_title;
    private String otheranwser;
    private int answerId=0;
    private Context context;
    private String userId, questionId;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    tv.setText("已提交");
                    break;

                default:
                    break;
            }
        }
    };
    private EditText message;
    private Gson gson;

    public CheckView(Context context) {
        super(context);
        initView(context);
    }

    public CheckView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    public void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.radioview, this, true);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        tv = (TextView) findViewById(R.id.sub);
        tv_title = (TextView) findViewById(R.id.title_essay);
    }
    public void initData(final List<EssayInfo.DataBean.ContentArrayBean.Answer>list, final Context context, String title, final String userId, final String questionId){
        tv_title.setText(title);
        this.context=context;
        for (int i = 0; i < list.size()+1; i++) {
            RadioButton tempButton = new RadioButton(context);
            tempButton.setPadding(80, 0, 0, 0);// 设置文字距离按钮四周的距离
            if (i==list.size()){
                tempButton.setText("其他");
                radioGroup.addView(tempButton, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            }else {
                tempButton.setText(list.get(i).getContent());
                radioGroup.addView(tempButton, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            }
        }
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton tempButton = (RadioButton)findViewById(checkedId);
                if (checkedId==list.size()+1){
                    showDialog();
                }else {
                    answerId=checkedId;
                }
            }
        });
        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                gson = new Gson();
                SendAnswer sendAnswer=new SendAnswer();
                sendAnswer.setAnswerId(answerId);
                sendAnswer.setOtherAnswer(otheranwser);
                List<Object>list1=new ArrayList<Object>();
                sendAnswer.setImgFiles(list1);
                String rebody= gson.toJson(sendAnswer);
                OkHttpTool.doPost(OkHttpTool.BASE_URL + "users/" + userId + "/question/" + questionId + "/answer",rebody, new OkHttpTool.setOkhttpListener() {
                    @Override
                    public void onSuccess(String s) {
                        handler.sendEmptyMessage(1);
                    }

                    @Override
                    public void onFail() {

                    }
                });
            }
        });
    }
    public void showDialog() {
        final CommentDialog commentDialog = new CommentDialog(context);
        message = commentDialog.getMessageEditText();
        commentDialog.setDialogclose(new OnClickListener() {
            @Override
            public void onClick(View v) {
                commentDialog.dismiss();
            }
        });
        commentDialog.setOnSendMessage(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (message.getText()!=null){
                    otheranwser=message.getText().toString();
                }
            }
        });

        commentDialog.show();
    }
}
